/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import java.util.Iterator;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;

/**
 * Web application lifecycle listener.
 * @author EhabMagdy
 */
public class E2raListener implements ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener, ServletRequestListener, ServletRequestAttributeListener {

    ServletContext context = null;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        context = sce.getServletContext();
        Session hSession = new Configuration().configure().buildSessionFactory().openSession();
        hSession.beginTransaction();
        Query catQuery = hSession.createSQLQuery("select distinct category from book");
        Query custQuery = hSession.createQuery("from pojo.Customer c order by c.id asc LIMIT 3");
        Query bkQuery = hSession.createQuery("from pojo.Book b order by b.isbn desc LIMIT 3");
        try {
            context.setAttribute("categories", catQuery.list());
            context.setAttribute("customers", custQuery.list());
            context.setAttribute("newbooks", bkQuery.list());
           // pojo.Customer c = (pojo.Customer) hSession.get(pojo.Customer.class, 1);
            //c.setType("admin");
            //context.setAttribute("user", c);
        } catch (Exception ex) {
            System.out.println("Queriers Failed");
        }
        hSession.close();
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }
}
